package com.customization.unionpay.contract.dao;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseContractSynDataDao {

    private static final Log logger = LogFactory.getLog(BaseContractSynDataDao.class);

    String SRHTTableName= "uf_skhttz"; //收入合同备案主表名
    String ZCHTTableName= "uf_zchttz"; //支出合同备案主表名
    String SZHTTableName= "formtable_main_289"; //TODO: 根据实际配置调整 收支两用合同备案主表名
    String HTZXTSTableName= "uf_htzxtsjl"; //合同中心推送记录表名
    String SRHTTZ_tableName="uf_skhttz"; //收入合同台账表
    String ZCHTTZ_tableName="uf_zchttz"; //支出合同台账表
    String SRHTJHTableName="uf_htskjhtz"; //合同收款计划台账表
    String ZCHTJHTableName="uf_htjhfktz"; //合同付款计划台账表

    String wfSKTableName = "formtable_main_187";//收入合同备案流程表名
    String wfZCTableName = "formtable_main_228";//支出合同备案流程表名


    public Integer addContractDataSynData(Map map) {
        List<String> paramList = new ArrayList<>(map.keySet());
        return addContractData(map,paramList,HTZXTSTableName);
    }

    public Map<String, String> selectContractBaseInfoByCntractNumber(String contractNumber) {
        Map<String,String> contractBaseInfo = this.selectSRContractBaseInfoByCntractNumber(contractNumber,SRHTTableName);
        if(null == contractBaseInfo){
            contractBaseInfo = this.selectZCContractBaseInfoByCntractNumber(contractNumber,ZCHTTableName);
        }
        return  contractBaseInfo;
    }

    /**
     * 查询收入合同基础备案信息
     * @param contractNumber
     * @param srhtTableName
     * @return
     */
    public Map<String, String> selectSRContractBaseInfoByCntractNumber(String contractNumber, String srhtTableName) {
        RecordSet rs = new RecordSet();
        String sql = "select htmc,htbh,htjbjgmc,htcjbbmmc,htcjbryxm,htgdr,htqdsj,htdqr,htskysbde,htfkysbde,yyyhtfj,zhtbh,glzht,htlsbz,htsfkgx,htywlx from "+srhtTableName+" where htbh = '" + contractNumber + "'";
        rs.executeSql(sql);
        if (rs.next()) {
            Map<String, String> data = new HashMap<>();
            data.put("htmc", rs.getString("htmc"));
            data.put("htbh", rs.getString("htbh"));
            data.put("htjbjgmc", rs.getString("htjbjgmc"));
            data.put("htcjbbmmc", rs.getString("htcjbbmmc"));
            data.put("htcjbryxm", rs.getString("htcjbryxm"));
            data.put("htgdr", rs.getString("htgdr"));
            data.put("htqdsj", rs.getString("htqdsj"));
            data.put("htdqr", rs.getString("htdqr"));
            data.put("htskysbde", rs.getString("htskysbde"));
            data.put("htfkysbde", rs.getString("htfkysbde"));
            data.put("yyyhtfj", rs.getString("yyyhtfj"));
            data.put("zhtbh", rs.getString("zhtbh"));
            data.put("glzht", rs.getString("glzht"));
            data.put("htlsbz", rs.getString("htlsbz"));
            data.put("htsfkgx", rs.getString("htsfkgx"));
            data.put("htywlx", rs.getString("htywlx"));
            return data;
        }
        return null;
    }

    /**
     * 查询支出合同基础备案信息
     * @param contractNumber
     * @param zchtTableName
     * @return
     */
    public Map<String, String> selectZCContractBaseInfoByCntractNumber(String contractNumber, String zchtTableName) {
        RecordSet rs = new RecordSet();
        String sql = "select htmc,htbh,htcbjgmc,htcbbmmc,htcbryxm,htgdr,htqdsj,htdqr,htskysbde,htfkysbde,yyyhtfj,zhtbh,glzht,sfjcht,sfsjdbmzxsfkjh,htlszygy,htlsbz,htsfkgx,htywlx from "+zchtTableName+" where htbh = '" + contractNumber + "'";
        rs.executeSql(sql);
        if (rs.next()) {
            Map<String, String> data = new HashMap<>();
            data.put("htmc", rs.getString("htmc"));
            data.put("htbh", rs.getString("htbh"));
            data.put("htcbjgmc", rs.getString("htcbjgmc"));
            data.put("htcbbmmc", rs.getString("htcbbmmc"));
            data.put("htcbryxm", rs.getString("htcbryxm"));
            data.put("htgdr", rs.getString("htgdr"));
            data.put("htqdsj", rs.getString("htqdsj"));
            data.put("htdqr", rs.getString("htdqr"));
            data.put("htskysbde", rs.getString("htskysbde"));
            data.put("htfkysbde", rs.getString("htfkysbde"));
            data.put("yyyhtfj", rs.getString("yyyhtfj"));
            data.put("zhtbh", rs.getString("zhtbh"));
            data.put("glzht", rs.getString("glzht"));
            data.put("sfjcht", rs.getString("sfjcht"));
            data.put("sfsjdbmzxsfkjh", rs.getString("sfsjdbmzxsfkjh"));
            data.put("htlszygy", rs.getString("htlszygy"));
            data.put("htlsbz", rs.getString("htlsbz"));
            data.put("htsfkgx", rs.getString("htsfkgx"));
            data.put("htywlx", rs.getString("htywlx"));
            return data;
        }
        return null;
    }

    public String selectZchttzIdByHtbh(String htbh){
        RecordSet rs = new RecordSet();
        String sql = "select id from "+ZCHTTZ_tableName+" where htbh = '" + htbh + "'";
        rs.executeSql(sql);
        if (rs.next()) {
            String id =  rs.getString("htmc");
            return id;
        }
        return null;
    }

    public String selectSrhttzIdByHtbh(String htbh){
        RecordSet rs = new RecordSet();
        String sql = "select id from "+SRHTTZ_tableName+" where htbh = '" + htbh + "'";
        rs.executeSql(sql);
        if (rs.next()) {
            String id =  rs.getString("htmc");
            return id;
        }
        return null;
    }

    /**
     * 获取移交合同信息
     * @param contractNumber
     * @return
     */
    public Map<String, String> selectYJContractBaseInfoByCntractNumber(String contractNumber) {
        // 查询收款合同信息
        Map<String,String> contractBaseInfo = this.selectYJSRContractBaseInfoByCntractNumber(contractNumber,SRHTTableName);
        if(null == contractBaseInfo){
            // 查询付款合同信息
            contractBaseInfo = this.selectYJZCContractBaseInfoByCntractNumber(contractNumber,ZCHTTableName);
        }
        return  contractBaseInfo;
    }

    /**
     * 查询收入合同移交基础信息
     * @param contractNumber
     * @param srhtTableName
     * @return
     */
    private Map<String, String> selectYJSRContractBaseInfoByCntractNumber(String contractNumber, String srhtTableName) {
        //TODO: 收入合同移交流程未提供表单
        return null;
    }
    /**
     * 查询支出合同移交基础信息
     * @param contractNumber
     * @param zchtTableName
     * @return
     */
    private Map<String, String> selectYJZCContractBaseInfoByCntractNumber(String contractNumber, String zchtTableName) {
        RecordSet rs = new RecordSet();
        String sql = "select htmc,htbh,htqdsj,htdqr,htfkysbde,glzht,htywlx,htzl,htbhsje,htjbjgmc as yhtjbjgmc,htjbbmmc as yhtjbbmmc,htjbryxm as yhtjbryxm from "+zchtTableName+" where htbh = '" + contractNumber + "'";
        rs.executeSql(sql);
        if (rs.next()) {
            Map<String, String> data = new HashMap<>();
            data.put("htmc", rs.getString("htmc"));
            data.put("htbh", rs.getString("htbh"));
            data.put("htqdsj", rs.getString("htqdsj"));
            data.put("htdqr", rs.getString("htdqr"));
            data.put("htfkysbde", rs.getString("htfkysbde"));
            data.put("glzht", rs.getString("glzht"));
            data.put("htywlx", rs.getString("htywlx"));
            data.put("htzl", rs.getString("htzl"));
            data.put("htbhsje", rs.getString("htbhsje"));
            data.put("yhtjbjgmc", rs.getString("yhtjbjgmc"));
            data.put("yhtjbbmmc", rs.getString("yhtjbbmmc"));
            data.put("yhtjbryxm", rs.getString("yhtjbryxm"));
            return data;
        }
        return null;
    }

    /**
     * 查询支出合同关闭基础信息
     * @param contractNumber
     * @return
     */
    public Map<String, String> selectGBContractBaseInfoByCntractNumber(String contractNumber) {
        RecordSet rs = new RecordSet();
        String sql = "select htmc,htbh,htqdsj,htdqr,htfkysbde,htywlx,htzl,htbhsje,htzt from "+ZCHTTableName+" where htbh = '" + contractNumber + "'";
        rs.executeSql(sql);
        if (rs.next()) {
            Map<String, String> data = new HashMap<>();
            data.put("htmc", rs.getString("htmc"));
            data.put("htbh", rs.getString("htbh"));
            data.put("htqdsj", rs.getString("htqdsj"));
            data.put("htdqr", rs.getString("htdqr"));
            data.put("htfkysbde", rs.getString("htfkysbde"));
            data.put("htywlx", rs.getString("htywlx"));
            data.put("htzl", rs.getString("htzl"));
            data.put("htbhsje", rs.getString("htbhsje"));
            data.put("htzt", rs.getString("htzt"));
            return data;
        }
        return null;
    }

    public Integer addSKContractDataLog(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractData(map,paramList,SRHTTZ_tableName);
    }

    public Integer addZCContractDataLog(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractData(map,paramList,ZCHTTZ_tableName);
    }

    public Integer addYXGXContractData(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractData(map,paramList,ZCHTTZ_tableName);
    }

    public Integer addYXGXContractPlanData(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractData(map,paramList,ZCHTTZ_tableName);
    }

    public boolean addYXGXContractSigatoriesData(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractDetailData(map,paramList,ZCHTTZ_tableName+"_dt1");
    }

    public boolean addFKContractSigatoriesData(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractDetailData(map,paramList,ZCHTTZ_tableName+"_dt1");
    }

    public boolean addYXGXContractDetailData(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        return addContractDetailData(map,paramList,ZCHTTZ_tableName+"_dt2");
    }

    public Integer addContractData(Map map,List<String> paramList,String tableName ){
        int dataId = 0;
        List<String> valueList = new ArrayList();
        List<String> params = new ArrayList();
        for (String param : paramList){
            params.add("?");
            String value = Util.null2String(map.get(param));
            if(StringUtils.isNotBlank(value)){
                valueList.add(value);
            }else{
                valueList.add(null);
            }
        }

        User user = new User(1);
        String modeuuid = "";
        StringBuilder insertSql = new StringBuilder();
        insertSql.append("insert into ").append(tableName).append(" (").append(String.join(",", paramList)).append(") values (").append(String.join(",", params)).append(")");
        logger.info("BaseContractSynDataDao addContractData() ==== insertSql=" + insertSql);
        RecordSet rs = new RecordSet();
        String sql = "select id from " + tableName +" where modeuuid = ?";
        rs.executeQuery(sql,modeuuid);
        if(rs.next()) {
            dataId = rs.getInt("id");
        }
        return dataId;
    }

    public boolean addContractDetailData(Map map,List<String> paramList,String tableName ){
        List<String> valueList = new ArrayList();
        List<String> params = new ArrayList();
        for (String param : paramList){
            params.add("?");
            String value = Util.null2String(map.get(param));
            if(StringUtils.isNotBlank(value)){
                valueList.add(value);
            }else{
                valueList.add(null);
            }
        }
        StringBuilder insertSql = new StringBuilder();
        insertSql.append("insert into ").append(tableName).append(" (").append(String.join(",", paramList)).append(") values (").append(String.join(",", params)).append(")");
        logger.info("BaseContractSynDataDao addContractDetailData() ==== insertSql=" + insertSql);
        return true;
    }

    public Map selectSKContractMain(String htbh,String tableName) {
        Map keyMap = getSKContarckMainMap();
        return selectDataByHtbh(keyMap,tableName,htbh);
    }

    public Map selectSKContractMainByRequestId(String tableName,Integer requestId) {
        Map keyMap = getSKContarckMainMap();
        return selectDataByRequestId(keyMap,tableName,requestId);
    }

    public Map selectXNContractMainByRequestId(String tableName,Integer requestId) {
        Map keyMap = getXNContarckMainMap();
        return selectDataByRequestId(keyMap,tableName,requestId);
    }

    private Map selectDataByHtbh(Map keyMap,String tableName,String htbh){
        List<String> keyList = Lists.newArrayList(keyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+ keyStr + " from "+tableName +" where htbh = ?";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql,htbh);
        Map result = new HashMap();
        if(recordSet.next()){
            keyList.forEach(key->{
                result.put(keyMap.get(key),recordSet.getString(key));
            });
        }else {
            return null;
        }
        return result;
    }

    private JSONArray selectDataPageByHtbh(Map keyMap,String tableName,Integer page, Integer pageSize,String condition){
        List<String> keyList = Lists.newArrayList(keyMap.keySet());
        String keyStr = String.join(",", keyList);
        RecordSet rs = new RecordSet();
        JSONArray jsonArray = new JSONArray();

        int offset = (page - 1) * pageSize;

        String sql = "select " +keyStr+ " from " + tableName  + " limit "+offset+","+pageSize+ " ";
        if(null != condition){
            sql += condition;
        }
        rs.executeSql(sql);
        while (rs.next()) {
            JSONObject result = new JSONObject();
            for(String key:keyList){
                result.put(key, rs.getString(key));
            }
            jsonArray.add(result);
        }
        return jsonArray;
    }

    private Map selectDataByRequestId(Map keyMap,String tableName,Integer requestId){
        List<String> keyList = Lists.newArrayList(keyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+ keyStr + " from "+tableName +" where requestid = ?";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql,requestId);
        Map result = new HashMap();
        if(recordSet.next()){
            keyList.forEach(key->{
                result.put(keyMap.get(key),recordSet.getString(key));
            });
        }else {
            return null;
        }
        return result;
    }

    private Map getSKContarckMainMap(){
        Map map = new HashMap();
        // map.put("id","mainid");
        map.put("htlx","htlx");
        map.put("zt","zt");
        map.put("jsbz","htqdbz");
        map.put("htmc","htmc");
        map.put("htbh","htbh");
        map.put("htcbjgmc","htcbjgmc");
        map.put("htcbbmmc","htcbbmmc");
        map.put("htcbryxm","htcbryxm");
        map.put("htgdr","htgdr");
        map.put("htqdsj","htqdsj");
        map.put("htdqr","htdqr");
        map.put("htskysbde","htskysbde");
        map.put("yyyhtfj","yyyhtfj");
        map.put("zhtbh","zhtbh");
        map.put("glzht","glzht");
        map.put("htlsbz","htlsbz");
        map.put("htsfkgx","htsfkgx");
        map.put("htywlx","htywlx");
        map.put("htzx","htzx");
        map.put("htzt","htzt");
        map.put("htzl","htzl");
        map.put("htbasj","htbasj");
        map.put("htglxx","htglxx");
        map.put("szpphtxx","szpphtxx");
        map.put("gljsrxnht","gljsrstht");
        map.put("srkjxy","srkjxy");
        map.put("htybzje","htybzje");
        map.put("hthl","hthl");
        map.put("htbhsje","htbhsje");
        map.put("hsdw","hsdw");
        map.put("hsbm","hsbm");
        map.put("sktj","sktj");
        // map.put("lcbh","htbalc");
        return map;
    }

    private Map getXNContarckMainMap(){
        Map map = new HashMap();
        map.put("htmc","htmc");
        map.put("htbh","htbh");
        map.put("htjbjg","htjbjg");
        map.put("htjbbm","htjbbm");
        map.put("htjbr","htjbr");
        map.put("htfkysbde","htfkysbde");
        map.put("htsfkgx","htsfkgx");
        map.put("htzx","htzx");
        map.put("htzt","htzt");
        map.put("htzl","htzl");
        map.put("htbasj","htbasj");
        map.put("htbhsje","htbhsje");
        map.put("fyhje","fyhje");
        // map.put("lcbh","htbalc");
        return map;
    }

    public Map selectFKContractMainByRequestId(Integer requestId,String tableName) {
        Map keyMap = getZCContarckMainMap();
        return selectDataByRequestId(keyMap,tableName,requestId);
    }

    public Map selectFKContractPlanMain(String htbh) {
        Map keyMap = getZCContarckMainMap();
        return selectDataByHtbh(keyMap,ZCHTTZ_tableName,htbh);
    }

    private Map getZCContarckMainMap() {
        Map map = new HashMap();
        // map.put("id","mainid");
        map.put("sfjcht","sfjc");
        map.put("yxdyqyfmc","yxdyqyfmc");
        map.put("htmc","htmc");
        map.put("htbh","htbh");
        map.put("htjbjg","htjbjg");
        map.put("htjbbm","htjbbm");
        map.put("htjbr","htjbr");
        map.put("htgdr","htgdr");
        map.put("htqdsj","htqdsj");
        map.put("htdqr","htdqr");
        map.put("htskysbde","htskysbde");
        map.put("htfkysbde","htfkysbde");
        map.put("yyyhtfj","yyyhtfj");
        map.put("zhtbh","zhtbh");
        map.put("glzht","glzht");
        map.put("sfsjdbmzhsfkjh","sfsjdbmzhsfkjh");
        map.put("htlsbz","htlsbz");
        map.put("htsfkgx","htsfkgx");
        map.put("htywlx","htywlx");
        map.put("htzx","htzx");
        map.put("htsjly","htsjly");
        map.put("htzt","htzt");
        map.put("htzl","htzl");
        map.put("htbasj","htbasj");
        map.put("htglxx","htglxx");
        map.put("szpphtxx","szpphtxx");
        map.put("yhdxyglxx","yhdxyglxx");
        map.put("gxedyxhtglxx","gxedyxhtglxx");
        map.put("htybzje","htybzje");
        map.put("htqdbz","htqdbz");
        map.put("hthl","hthl");
        map.put("htrmbzje","htrmbzje");
        map.put("htbhsje","htbhsje");
        map.put("fyhje","fyhje");
        map.put("zbhje","zbhje");
        map.put("zjgcxm","zjgcxm");
        map.put("zlje","zlje");
        map.put("zlmj","zlmj");
        map.put("zldz","zldz");
        map.put("zlksrq","zlksrq");
        map.put("zljsrq","zljsrq");
        map.put("fkzq","fkzq");
        map.put("mzksrq","mzksrq");
        map.put("mzjsrq","mzjsrq");
        map.put("sfcslc","sfcslc");
        map.put("pfxbfj","pfhxbhfj");
        map.put("pfhxbh","pfhxbh");
        // map.put("lcbh","htbalc");
        // map.put("htglbhwb","htglbhwb");
        return map;
    }

    public List<Map> selectSKJHContractMX(String htbh,String mainTableName,String detailTableName) {
        Map skjhKeyMap = getSKJHContarckMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+detailTableName+" where mainId = ( SELECT id FROM "+mainTableName+" WHERE htbh = " + htbh + ")";
        recordSet.execute(sql);
        List skhtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(key,recordSet.getString(key));
            });
            skhtjh.add(result);
        }
        return skhtjh;
    }

    public List<Map> selectSKJHContractMXByRequestId(Integer requestId,String mainTableName,String detailTableName) {
        Map skjhKeyMap = getSKJHContarckMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+detailTableName+" where mainId = ( SELECT id FROM "+mainTableName+" WHERE requestid = " + requestId + ")";
        recordSet.execute(sql);
        List skhtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(skjhKeyMap.get(key),recordSet.getString(key));
            });
            skhtjh.add(result);
        }
        return skhtjh;
    }

    public List<Map> selectZCJHVirtualContractMXByRequestId(Integer requestId,String mainTableName,String detailTableName) {
        Map skjhKeyMap = getSKJHContarckMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+detailTableName+" where mainId = ( SELECT id FROM "+mainTableName+" WHERE requestid = " + requestId + ")";
        recordSet.execute(sql);
        List skhtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(skjhKeyMap.get(key),recordSet.getString(key));
            });
            skhtjh.add(result);
        }
        return skhtjh;
    }

    private Map getSKJHContarckMap() {
        Map map = new HashMap();
        map.put("ftgz","ftgz");
        map.put("lykssj","lykssj");
        map.put("lyjssj","lyjssj");
        map.put("dzjszq","dzjszq");
        map.put("jhskrq","jhskrq");
        map.put("skhtlbsrywlxdl","skhtlbsrywdl");
        map.put("srywlxzsrwlxxl","srywlxsrwxl");
        map.put("jjsx","jjsx");
        map.put("km","hskm");
        map.put("jhskje","jhskje");
        map.put("sl","kdksl");
        map.put("jhfkbhsje","jhskbhsje");
        map.put("jhzrbm","jhzrbm");
        map.put("jhzrr","jhzrr");
        map.put("skjhbh","sfkjhbh");
        return map;

    }


    public List<Map> selectFKJHContractMXByRequestId(Integer requestId,String mainTableName,String detailTableName) {
        Map skjhKeyMap = getFKJHContarckMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+detailTableName+" where mainId = ( SELECT id FROM "+mainTableName+" WHERE requestid = ? )";
        recordSet.executeQuery(sql,requestId);
        List zchtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(skjhKeyMap.get(key),recordSet.getString(key));
            });
            zchtjh.add(result);
        }
        return zchtjh;
    }

    private Map getFKJHContarckMap() {
        Map map = new HashMap();
        map.put("sfjsjs","sfjsjs");
        map.put("fkxz","fkxz");
        map.put("zchtlbzcywlxdl","zchtdl");
        map.put("zcywlxzcywlxxl","zcywxl");
        map.put("ysxm","ysxm");
        map.put("zdgz","zdgz");
        map.put("jjsx","jjsx");
        map.put("yskm","yskm");
        map.put("cbxz","cbxz");
        map.put("jhfkrq","jhfkrq");
        map.put("fktj","fktj");
        map.put("jhfkje","jhfkje");
        map.put("zpsl","zpsl");
        map.put("jhfkbhsje","jhfkbhsje");
        map.put("fyhje","fyhje");
        map.put("zbhje","zbhje");
        map.put("jhzrbm","jhzrbm");
        map.put("jhzrr","jhzrr");
        map.put("lyksrq","lyksrq");
        map.put("lyjsrq","lyjsrq");
        map.put("fkjhbh","sfkjhbh");
        return map;
    }

    public boolean addZCJHContractDataLog(Map main, List<Map> fkjhContractMX) {

        fkjhContractMX.forEach(map -> {
            map.putAll(main);
            List<String> paramList = Lists.newArrayList(map.keySet());
            addContractPlanData(map,paramList,ZCHTJHTableName);
        });
        return true;
    }

    public boolean addSKJHContractDataLog(Map main, List<Map> skjhContractMX) {
        skjhContractMX.forEach(map -> {
            map.putAll(main);
            List<String> paramList = Lists.newArrayList(map.keySet());
            addContractPlanData(map,paramList,SRHTJHTableName);
        });
        return true;
    }

    public boolean addContractPlanData(Map map,List<String> paramList,String tableName ) {
        List<String> valueList = new ArrayList();
        List<String> params = new ArrayList();
        for (String param : paramList){
            params.add("?");
            String value = Util.null2String(map.get(param));
            if(StringUtils.isNotBlank(value)){
                valueList.add(value);
            }else{
                valueList.add(null);
            }
        }

        User user = new User(1);
        StringBuilder insertSql = new StringBuilder();
        insertSql.append("insert into ").append(tableName).append(" (").append(String.join(",", paramList)).append(") values (").append(String.join(",", params)).append(")");
        logger.info("BaseContractSynDataDao addContractPlanData() ==== insertSql=" + insertSql);
        return true;
    }

    public Map selectSFKContractSKMain(String htbh) {
        Map keyMap = getSZContarckSKMainMap();
        return selectDataByHtbh(keyMap,SZHTTableName,htbh);
    }

    private Map getSZContarckSKMainMap() {
        Map map = new HashMap();
        map.put("htmc","htmc");
        map.put("htbh","htbh");
        map.put("htjbjg","htcbjgmc");
        map.put("htjbbm","htcbbmmc");
        map.put("htjbr","htcbryxm");
        map.put("htgdr","htgdr");
        map.put("htqdsj","htqdsj");
        map.put("htdqr","htdqr");
        map.put("htskysbde","htskysbde");
        map.put("yyyhtfj","yyyhtfj");
        map.put("zhtbh","zhtbh");
        map.put("glzht","glzht");
        map.put("htlsbz","htlsbz");
        map.put("htsfkgx","htsfkgx");
        map.put("htywlx","htywlx");
        map.put("htzx","htzx");
        map.put("htzt","htzt");
        map.put("htzl","htzl");
        map.put("htbasj","htbasj");
        map.put("htglxx","htglxx");
        map.put("szpphtxx","szpphtxx");
        map.put("htybzje","htybzje");
        map.put("hthl","hthl");
        map.put("htbhsje","htbhsje");
        map.put("hsdw","hsdw");
        map.put("ztbm","hsbm");
        return map;
    }

    public List<Map> selectSFKJHContractSKMX(String htbh) {
        Map skjhKeyMap = getSFKJHContarcSFKMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+SZHTTableName+"_dt2 where mainId = ( SELECT id FROM "+SZHTTableName+" WHERE htbh = " + htbh + ")";
        recordSet.executeSql(sql);
        List srhtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(key,recordSet.getString(key));
            });
            srhtjh.add(result);
        }
        return srhtjh;
    }

    private Map getSFKJHContarcSFKMap() {
        Map map = new HashMap();
        map.put("ftgz","ftgz");
        map.put("lykssj","lykssj");
        map.put("lyjssj","lyjssj");
        map.put("dzjszq","dzjszq");
        map.put("jhskrq","jhskrq");
        map.put("skhtlbsrywdl","srhtdl");
        map.put("srywlxsrwxl","srywxl");
        map.put("jjsx","jjsx");
        map.put("hskm","km");
        map.put("sktj","sktj");
        map.put("jhskje","jhskje");
        map.put("kdksl","sl");
        map.put("jhskbhsje","jhskbhsje");
        map.put("jhzrbm","jhzrbm");
        map.put("jhzrr","jhzrr");
        return map;
    }

    public Map selectSFKContractFKMainByRequestId(Integer requestId) {
        Map keyMap = getSZContarckFKMainMap();
        return selectDataByRequestId(keyMap,SZHTTableName,requestId);
    }

    private Map getSZContarckFKMainMap() {
        Map map = new HashMap();
        map.put("sfjcht","sfjc");
        map.put("htmc","htmc");
        map.put("htbh","htbh");
        map.put("htjbjg","htjbjg");
        map.put("htjbbm","htjbbm");
        map.put("htjbr","htjbr");
        map.put("htgdr","htgdr");
        map.put("htqdsj","htqdsj");
        map.put("htdqr","htdqr");
        map.put("htskysbde","htskysbde");
        map.put("htfkysbde","htfkysbde");
        map.put("yyyhtfj","yyyhtfj");
        map.put("zhtbh","zhtbh");
        map.put("glzht","glzht");
        map.put("sfsjdbmzhsfkjh","sfsjdbmzhsfkjh");
        map.put("htlsbz","htlsbz");
        map.put("htsfkgx","htsfkgx");
        map.put("htywlx","htywlx");
        map.put("htzx","htzx");
        map.put("htzt","htzt");
        map.put("htzl","htzl");
        map.put("htbasj","htbasj");
        map.put("htglxx","htglxx");
        map.put("szpphtxx","szpphtxx");
        map.put("yhdxyglxx","yhdxyglxx");
        map.put("gxedyxhtglxx","gxedyxhtglxx");
        map.put("htybzje","htybzje");
        map.put("htqdbz","htqdbz");
        map.put("hthl","hthl");
        map.put("htrmbzje","htrmbzje");
        map.put("htbhsje","htbhsje");
        map.put("fyhje","fyhje");
        map.put("zbhje","zbhje");
        map.put("hsdw","hsdw");
        map.put("ztbm","hsbm");
        map.put("htglbhwb","htglbhwb");
        return map;
    }

    public List<Map> selectSFKJHContractFKMXByRequestId(Integer requestId) {
        Map fkjhKeyMap = getSFKJHContarckFKMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(fkjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+SZHTTableName+"_dt1 where mainId = ( SELECT id FROM "+SZHTTableName+" WHERE requestid = " + requestId + ")";
        recordSet.executeSql(sql);
        List zchtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(key,recordSet.getString(key));
            });
            zchtjh.add(result);
        }
        return zchtjh;
    }

    private Map getSFKJHContarckFKMap() {
        Map map = new HashMap();
        map.put("sfjsjs","sfjsjs");
        map.put("fkxz","fkxz");
        map.put("zchtdl","zchtdl");
        map.put("zcywxl","zcywxl");
        map.put("ysxm","ysxm");
        map.put("zdgz","zdgz");
        map.put("jjsx","jjsx");
        map.put("yskm","yskm");
        map.put("cbxz","cbxz");
        map.put("jhfkrq","jhfkrq");
        map.put("fktj","fktj");
        map.put("jhfkje","jhfkje");
        map.put("zpsl","zpsl");
        map.put("jhfkbhsje","jhfkbhsje");
        map.put("fyhje","fyhje");
        map.put("zbhje","zbhje");
        map.put("jhzrbm","jhzrbm");
        map.put("jhzrr","jhzrr");
        return map;
    }

    public JSONObject selectSKContractMX(String htbh, int i) {
        return selectContractMX(htbh,SRHTTableName,i);
    }

    public JSONObject selectFKContractMX(String htbh, int i) {
        return selectContractMX(htbh,ZCHTTableName,i);
    }

    public JSONObject selectContractMX(String htbh,String tableName, int i) {
        RecordSet recordSet = new RecordSet();
        String mxTB = SRHTTableName+"_dt"+i;
        String sql = "select * from "+mxTB+" where mainId = ( SELECT id FROM "+tableName+" WHERE htbh = " + htbh + ")";
        recordSet.executeSql(sql);
        JSONArray jsonArray = new JSONArray();
        while (recordSet.next()){
            List<String> keyList = Lists.newArrayList(recordSet.getColumnName());
            JSONObject result = new JSONObject();
            keyList.forEach(key->{
                result.put(key,recordSet.getString(key));
            });
            jsonArray.add(result);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(mxTB,jsonArray);
        return jsonObject;
    }

    public boolean skContractTransfer(String htbh, int xhtjbryxm, String xhtjbjgmc, String xhtjbbmmc) {
        String updateSql = "update "+ SRHTTZ_tableName + " set htcbryxm='"+xhtjbryxm +"', htcbjgmc='"+xhtjbjgmc+"', htcbbmmc='"+xhtjbbmmc + "' where htbh='"+htbh+"'";
        logger.info("BaseContractSynDataDao skContractTransfer() ==== updateSql=" + updateSql);
        RecordSet recordSet = new RecordSet();
        // 更新
        return recordSet.executeUpdate(updateSql);
    }

    public boolean fkContractTransfer(String htbh, int xhtjbryxm, String xhtjbjgmc, String xhtjbbmmc) {
        String updateSql = "update "+ ZCHTTZ_tableName + " set htjbr='"+xhtjbryxm +"', htjbjg='"+xhtjbjgmc+"', htjbbm='"+xhtjbbmmc + "' where htbh='"+htbh+"'";
        logger.info("BaseContractSynDataDao fkContractTransfer() ==== updateSql=" + updateSql);
        RecordSet recordSet = new RecordSet();
        // 更新
        return recordSet.executeUpdate(updateSql);
    }

    public boolean contractClose(String htbh) {
        String updateSql = "update "+ ZCHTTZ_tableName + " set htzt=5 where htbh='"+htbh+"'";
        logger.info("BaseContractSynDataDao contractClose() ==== updateSql=" + updateSql);
        RecordSet recordSet = new RecordSet();
        // 更新
        return recordSet.executeUpdate(updateSql);
    }

    public Map<String, String> selectFKContractVirtualMain(String htbh) {
        Map keyMap = new HashMap();
        keyMap.put("htbh","htbh");
        keyMap.put("htjbr","htjbr");
        keyMap.put("htmc","htmc");
        keyMap.put("htje","htje");
        return selectDataByHtbh(keyMap,ZCHTTZ_tableName,htbh);
    }

    public Integer selectJTContractZchttzTotal() {
        String sql = "select count(1) from "+ZCHTTZ_tableName +"where htsfkgx = 2 and htzt in (0,1) and htglxx != 2 and htzl != 2 and htzt in (1,4) ";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        Integer result = 0;
        if(recordSet.next()){
            result = recordSet.getInt(0);
        }
        return result;
    }

    public JSONArray selectZchttzByPage(Integer page, Integer pageSize) {
        Map keyMap = getZCContarckJTMainMap();
        String condition = "where htsfkgx = 2 and htzt in (0,1) and htglxx != 2 and htzl != 2 and htzt in (1,4) ";
        return selectDataPageByHtbh(keyMap,ZCHTTZ_tableName,page,pageSize,condition);
    }

    private Map getZCContarckJTMainMap() {
        Map map = new HashMap();
        map.put("htmc","htmc");
        map.put("htbh","htbh");
        map.put("htjbjg","htjbjg");
        map.put("htjbbm","htjbbm");
        map.put("htjbr","htjbr");
        map.put("htqdsj","htqdsj");
        map.put("htdqr","htdqr");
        map.put("htfkysbde","htfkysbde");
        map.put("glzht","glzht");
        map.put("htywlx","htywlx");
        map.put("htzl","htzl");
        map.put("htbhsje","htbhsje");
        return map;
    }

    public JSONArray selectZchttzMXByHtbh(String htbh) {
        Map map = new HashMap();
        map.put("sfylf","sfylf");
        map.put("qyfmc","qyfmc");
        map.put("qyftyshxydm","qyftyshxydm");
        return selectZBDataJsonListByHtbh(map,ZCHTTZ_tableName+"_dt1",htbh);
    }

    private JSONArray selectZBDataJsonListByHtbh(Map keyMap,String tableName,String htbh){
        List<String> keyList = Lists.newArrayList(keyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+ keyStr + " from "+tableName +" where mainid = ( SELECT id FROM "+tableName+" WHERE htbh = ?)";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql,htbh);
        JSONArray jsonArray = new JSONArray();
        while (recordSet.next()){
            JSONObject result = new JSONObject();
            keyList.forEach(key->{
                result.put(keyMap.get(key)+"",recordSet.getString(key));
            });
            jsonArray.add(result);
        }
        return jsonArray;
    }

    private JSONArray selectDataJsonListByHtbh(Map keyMap,String tableName,String htbh){
        List<String> keyList = Lists.newArrayList(keyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+ keyStr + " from "+tableName +" where htbh = ?";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql,htbh);
        JSONArray jsonArray = new JSONArray();
        while (recordSet.next()){
            JSONObject result = new JSONObject();
            keyList.forEach(key->{
                result.put(keyMap.get(key)+"",recordSet.getString(key));
            });
            jsonArray.add(result);
        }
        return jsonArray;
    }

    public JSONArray selectFkjhtzByHtbh(String htbh) {
        Map map = new HashMap();
        map.put("sfkjhbh","sfkjhbh");
        map.put("sfjsjs","sfjsjs");
        map.put("fkxz","fkxz");
        map.put("zchtdl","zchtdl");
        map.put("zcywxl","zcywxl");
        map.put("ysxm","ysxm");
        map.put("zdgz","zdgz");
        map.put("jjsx","jjsx");
        map.put("yszrzx","yszrzx");
        map.put("yskm","yskm");
        map.put("cbxz","cbxz");
        map.put("fktj","fktj");
        map.put("jhfkje","jhfkje");
        map.put("zpsl","zpsl");
        map.put("jhfkbhsje","jhfkbhsje");
        map.put("fyhje","fyhje");
        map.put("zbhje","zbhje");
        map.put("jhzrjg","jhzrbm");
        map.put("jhzrbm","jhzrr");
        map.put("jhzrr","jtftje");
        return selectDataJsonListByHtbh(map,ZCHTJHTableName,htbh);
    }

    public List<Map> selectFKHTSignatoriesByRequestId(Integer requestId, String mainTableName) {
        String detailTableName = mainTableName + "_dt1";
        Map skjhKeyMap = getFKJHSignatoriesMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+detailTableName+" where mainId = ( SELECT id FROM "+mainTableName+" WHERE requestid = ? )";
        recordSet.executeQuery(sql,requestId);
        List zchtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(skjhKeyMap.get(key),recordSet.getString(key));
            });
            zchtjh.add(result);
        }
        return zchtjh;
    }

    private Map getFKJHSignatoriesMap() {
        Map map = new HashMap();
        map.put("sfylf","sfylf");
        map.put("qyfmc","qyfmc");
        map.put("qyftyshxydm","qyftyshxydm");
        map.put("khyh","khyh");
        map.put("zh","zh");
        map.put("yhzh","yhzh");
        // map.put("qyfmcwb","qyfmcwb");
        map.put("qyfbm","qyfbm");
        return map;
    }

    public List<Map> selectSKHTSignatoriesByRequestId(Integer requestId, String mainTableName) {
        String detailTableName = mainTableName + "_dt1";
        Map skjhKeyMap = getSKJHSignatoriesMap();
        RecordSet recordSet = new RecordSet();
        List<String> keyList = Lists.newArrayList(skjhKeyMap.keySet());
        String keyStr = String.join(",", keyList);
        String sql = "select "+keyStr+" from "+detailTableName+" where mainId = ( SELECT id FROM "+mainTableName+" WHERE requestid = ? )";
        recordSet.executeQuery(sql,requestId);
        List srhtjh = Lists.newArrayList();
        while (recordSet.next()){
            Map result = new HashMap();
            keyList.forEach(key->{
                result.put(skjhKeyMap.get(key),recordSet.getString(key));
            });
            srhtjh.add(result);
        }
        return srhtjh;
    }


    private Map getSKJHSignatoriesMap() {
        Map map = new HashMap();
        map.put("sfylf","sfylf");
        map.put("qyfmc","qyfmc");
        map.put("qyftyshxydm","qyftyshxydm");
        map.put("khyh","khyh");
        map.put("zh","zh");
        map.put("yhzh","yhzh");
        map.put("qyfmcwb","qyfmcwb");
        map.put("qyfbm","qyfbm");
        return map;
    }

    public boolean addSKContractSigatoriesData(Map map) {
        List<String> paramList = Lists.newArrayList(map.keySet());
        addContractDetailData(map,paramList,SRHTTZ_tableName+"_dt1");
        return true;
    }
}
